import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";

import { Observable, of } from "rxjs";


@Injectable({
    providedIn: 'root'
})
export class MenuManageService {

    private baseUrl: string = "/api/menus"
    private allTreeUrl:string = '/api/menus/tree'
    private allFrontTreeUrl:string="/api/menus/build"

    constructor(
        private http: HttpClient
    ) { }

    apiList(data): Observable<any> {
        let _dataJson = {
            params: {
                ...data
            }
        }
        return this.http.get(this.baseUrl, _dataJson)
    }
    apiBuildList():Observable<any>{
        return this.http.get(this.allFrontTreeUrl)
    }
    apiEdit(data): Observable<any> {
        let _dataJson = {
            ...data,
            pid : !data.pid ? 0 : data.pid
        }
        return this.http.put(this.baseUrl, _dataJson).pipe();
    }
    apiAdd(data): Observable<any> {
        let _dataJson = {
            ...data,
            pid : !data.pid ? 0 : data.pid
        }
        return this.http.post(this.baseUrl, _dataJson).pipe();
    }
    apiDelete(data): Observable<any> {
        let _dataJson = {
            headers: new HttpHeaders({
                'Content-Type': 'application/json',
            }),
            body: data
        }
        return this.http.delete(this.baseUrl, _dataJson).pipe();
    }

    // 角色管理页面使用
    apiGetAllMenu(data) : Observable<any>{
        return this.http.get(this.allTreeUrl, data)
    }

}
